package main

import (
	"fmt"
	"log"
	"net/http"
	// "syscall"

	// "github.com/fvbock/endless"

	"dolphin/seaweed/models"
	"dolphin/seaweed/pkg/gredis"
	"dolphin/seaweed/pkg/logging"
	"dolphin/seaweed/pkg/setting"
	"dolphin/seaweed/routers"
)

// @title Golang Gin API
// @version 1.0
// @description An example of gin
// @termsOfService https://github.com/EDDYCJY/go-gin-example

// @license.name MIT
// @license.url https://github.com/EDDYCJY/go-gin-example/blob/master/LICENSE
func main() {
	setting.Setup()
	models.Setup()
	logging.Setup()
	gredis.Setup()

	routersInit := routers.InitRouter()
	readTimeout := setting.ServerSetting.ReadTimeout
	writeTimeout := setting.ServerSetting.WriteTimeout
	endPoint := fmt.Sprintf(":%d", setting.ServerSetting.HttpPort)
	maxHeaderBytes := 1 << 20

	// If it is windows, you should open and comment out the endless related code.
	server := &http.Server{
		Addr:           endPoint,
		Handler:        routersInit,
		ReadTimeout:    readTimeout,
		WriteTimeout:   writeTimeout,
		MaxHeaderBytes: maxHeaderBytes,
	}
	//
	//server.ListenAndServe()
	//return

	// endless.DefaultReadTimeOut = readTimeout
	// endless.DefaultWriteTimeOut = writeTimeout
	// endless.DefaultMaxHeaderBytes = maxHeaderBytes
	// server := endless.NewServer(endPoint, routersInit)
	// server.BeforeBegin = func(add string) {
	// 	log.Printf("Actual pid is %d", syscall.Getpid())
	// }

	err := server.ListenAndServe()
	if err != nil {
		log.Printf("Server err: %v", err)
	}
}
